package com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leave.servlet;

import com.alibaba.fastjson.JSON;
import com.yunzheng.studyLeaveSystem.modules.response.ResponseWrapper;
import com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leave.entity.Leave;
import com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leave.service.LeaveService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * @Author: YunZheng
 * @Date: 2023/01/30/10:53
 * @Description:
 */
@WebServlet(name = "LeaveServlet",value = "/LeaveServlet")
public class LeaveServlet extends HttpServlet {
    LeaveService leaveService = new LeaveService();
    /**
     * 接收请假请求
     * @param req 请求
     * @param resp 响应
     * @throws IOException 抛出异常
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.setAttribute("id",7);
        String ID = session.getAttribute("id").toString();
        int id = Integer.parseInt(ID);
        BufferedReader reader = req.getReader();
        String readline = reader.readLine();
        Leave leave = JSON.parseObject(readline,Leave.class);
        leaveService.leave(leave,id);
        ResponseWrapper responseWrapper = ResponseWrapper.SuccessButNoData();
        String jsonString = JSON.toJSONString(responseWrapper);
        resp.getWriter().println(jsonString);
        System.out.println("已返回状态码：" + jsonString);
    }

    /**
     * 接收请假请求
     * @param req 请求
     * @param resp 响应
     * @throws IOException 抛出异常
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}